unit UserManagementForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDF, uADStanIntf, uADStanOption,
  uADStanError, uADGUIxIntf, uADPhysIntf, uADStanDef, uADStanPool, uADStanAsync,
  uADPhysManager, Data.DB, uADCompClient, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Graphics,
  Vcl.DBCtrls, Vcl.Grids, Vcl.DBGrids, uADStanParam, uADDatSManager,
  uADDAptIntf, uADDAptManager, uADGUIxFormsWait, uADCompGUIx, uADCompDataSet,
  uADPhysIB, uADPhysMySQL;

type
  TfmUserManagement = class(TForm)
    DBGroupID: TDBLookupComboBox;
    Label1: TLabel;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    txtPassword: TEdit;
    txtPassword2: TEdit;
    txtUsername: TEdit;
    txtFirstName: TEdit;
    txtLastName: TEdit;
    txtNickName: TEdit;
    ADConnection1: TADConnection;
    ADPhysIBDriverLink1: TADPhysIBDriverLink;
    adtUsers: TADTable;
    ADQuery1: TADQuery;
    dsUsers: TDataSource;
    ADGUIxWaitCursor1: TADGUIxWaitCursor;
    btnNew: TButton;
    btnSave: TButton;
    btnClose: TButton;
    btnDelete: TButton;
    adtGroups: TADTable;
    dsGroups: TDataSource;
    ADPhysMySQLDriverLink1: TADPhysMySQLDriverLink;
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnCloseClick(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
    procedure DBGroupIDDropDown(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure btnSaveClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
  private
    { Private declarations }
    DBOp: TDBOp;
  public
    { Public declarations }
  end;

var
  fmUserManagement: TfmUserManagement;

implementation

{$R *.dfm}

procedure TfmUserManagement.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfmUserManagement.btnDeleteClick(Sender: TObject);
begin
  if adtUsers.FieldByName('ID').AsInteger < 2 then
  begin
    ShowMessage('Can''t delete Admin user!');
    Exit;
  end;
  with ADQuery1 do
  begin
    SQL.Clear;
    Params.Clear;
    SQL.Add('Delete From Users Where ID=:ID;');
    Params.ParamByName('ID').AsInteger := adtUsers.FieldByName('ID').AsInteger;
  end;
  DoQuerySQL(ADConnection1, ADQuery1);
  adtUsers.Refresh;
  btnNew.Click;
end;

procedure TfmUserManagement.btnNewClick(Sender: TObject);
begin
  txtUsername.Text := '';
  txtPassword.Text := '';
  txtPassword2.Text := '';
  txtFirstName.Text := '';
  txtLastName.Text := '';
  txtNickName.Text := '';
  DBGroupID.KeyValue := VarEmpty;
  DBOp := opInsert;
end;

procedure TfmUserManagement.btnSaveClick(Sender: TObject);
begin
  if txtPassword.Text <> txtPassword2.Text then
  begin
    ShowMessage('Password is not identical!');
    txtPassword.SetFocus;
    Exit;
  end;

  with ADQuery1 do
  begin
    SQL.Clear;
    Params.Clear;
    case DBOp of
      opInsert:
        SQL.Add('Insert Into users (Username, Pass, GroupID, FirstName, ' +
                'LastName, Nickname, CreateDate, ModifyDate) ' +
                'Values (:Username, :Pass, :GroupID, :FirstName, :LastName, ' +
                ':Nickname, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);');
      opUpdate: begin
        SQL.Add('Update users Set GroupID=:GroupID, FirstName=:FirstName, ' +
                'LastName=:LastName, Nickname=:Nickname, ModifyDate=CURRENT_TIMESTAMP ');
        if txtPassword.Text <> '********' then
          SQL.Add(', Pass=:Pass');
        SQL.Add(' Where Username=:Username;');
      end;
    end;
    Params.ParamByName('Username').AsString := Trim(txtUsername.Text);
    if Params.FindParam('Pass') <> nil then
      Params.ParamByName('Pass').AsString := MD5(Trim(txtPassword.Text));
    Params.ParamByName('GroupID').Value := DBGroupID.KeyValue;
    Params.ParamByName('FirstName').AsString := Trim(txtFirstName.Text);
    Params.ParamByName('LastName').AsString := Trim(txtLastName.Text);
    Params.ParamByName('Nickname').AsString := Trim(txtNickName.Text);
  end;
  DoQuerySQL(ADConnection1, ADQuery1);
  adtUsers.Refresh;
  btnNew.Click;
end;

procedure TfmUserManagement.DBGrid1CellClick(Column: TColumn);
begin
  txtUsername.Text := adtUsers.FieldByName('Username').AsString;
  txtPassword.Text := '********';
  txtPassword2.Text := '********';
  txtFirstName.Text := adtUsers.FieldByName('FirstName').AsString;
  txtLastName.Text := adtUsers.FieldByName('LastName').AsString;
  txtNickName.Text := adtUsers.FieldByName('NickName').AsString;
  DBGroupID.KeyValue := adtUsers.FieldByName('GroupID').AsVariant;
  DBOp := opUpdate;
end;


procedure TfmUserManagement.DBGroupIDDropDown(Sender: TObject);
begin
  adtGroups.Last;
  adtGroups.First;
end;

procedure TfmUserManagement.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  CanClose := True;
  FreeAndNil(fmUserManagement);
end;

procedure TfmUserManagement.FormCreate(Sender: TObject);
var
  ADPhysDriverLink: TADPhysDriverLink;
  DriverID: string;
begin
  DriverID := DBDriverID;
  if DriverID = 'Firebird' then
    ADPhysDriverLink := ADPhysIBDriverLink1;
  if DriverID = 'MySQL' then
    ADPhysDriverLink := ADPhysMySQLDriverLink1;
  SetDBInfo(ADConnection1, ADPhysDriverLink);
  adtUsers.Active := True;
  adtGroups.Active := True;
end;

end.
